# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=osgworks

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn")
_ver_base=3.1.0
pkgver=3.1.0.559
pkgrel=1
pkgdesc="A toolkit for OpenSceneGraph applications. (mingw-w64)"
arch=('any')
license=('custom')
url="https://code.google.com/p/osgworks/"
depends=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph")
makedepends=("subversion" "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake")
options=(!strip staticlibs !buildflags)
source=("${_realname}"::"svn+http://osgworks.googlecode.com/svn/trunk/"
        "osg-3.3.x.patch"
        "mingw-fpc-libs.patch")
md5sums=('SKIP'
         'cc8a517171bcd4c9a080c81a6bd757f0'
         'c5b52fc01efde31fc87d8ed0450bdd8f')

pkgver() {
  cd "$srcdir/$_realname"
  local ver="$(svnversion)"
  printf "%s.%s" "${_ver_base}" "${ver//[[:alpha:]]}"
}

prepare() {
  cd "$srcdir/$_realname"
  patch -p1 -i ${srcdir}/osg-3.3.x.patch
  patch -p1 -i ${srcdir}/mingw-fpc-libs.patch
}

build() {
  plain "No build step"
}

package_release() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  
  [[ -d ${srcdir}/build-release ]] && rm -rf ${srcdir}/build-release
  mkdir -p ${srcdir}/build-release && cd ${srcdir}/build-release

  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}
  
  make
  make install

  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  #for ff in ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/*.pc; do
  #  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i $ff
  #done
}

package_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn=$pkgver" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  
  [[ -d ${srcdir}/build-debug ]] && rm -rf ${srcdir}/build-debug
  mkdir -p ${srcdir}/build-debug && cd ${srcdir}/build-debug

  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Debug \
    ../${_realname}
  
  make
  make install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/flagpoll
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/*.cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-osgworks-svn() {
  package_release
}

package_mingw-w64-i686-osgworks-debug-svn() {
  package_debug
}

package_mingw-w64-x86_64-osgworks-svn() {
  package_release
}

package_mingw-w64-x86_64-osgworks-debug-svn() {
  package_debug
}

